<?php

namespace App\Models\Dynamic;

use App\User;
use Illuminate\Database\Eloquent\Model;

class BloodTest extends Model
{
    protected $table = "tn_blood_test";

    public $timestamps = false;
    protected $fillable = [
        'ACCOUNT', 'TYPE', 'COUNTS', 'CREATETIME', 'DIETSITUATION', 'DIETPIC', 'MEDICATIONSITUATION', 'MEDICATIONPIC', 'HIGHESTHYPERTENSION',
        'LOWESTHYPERTENSION', 'HEARTRATE', 'WEIGHT', 'ISABNORMAL', 'REMARK', 'ADDTIME'
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function user()
    {
        return $this->hasMany(User::class);
    }


    public function getThreeValue($start_time, $end_time, $type, $account, $max_value, $min_value)
    {
        $result = array();

        //1餐前 2餐后 3睡前 4凌晨
        switch ($type) {
            case 1:
                $in_type = ['0', '2', '4'];
                break;
            case 2:
                $in_type = ['1', '3', '5'];
                break;
            case 3:
                $in_type = ['6'];
                break;
            case 4:
                $in_type = ['7'];
                break;
        }

        $result['normal'] = $this->where('ACCOUNT', '=', $account)
            ->whereBetween('COUNTS', [$min_value, $max_value])
            ->whereBetween('CREATETIME', [$start_time, $end_time])
            ->whereIn('TYPE', $in_type)
            ->count();

        $result['low'] = $this->where('ACCOUNT', '=', $account)
            ->where('COUNTS', '<', $min_value)
            ->whereBetween('CREATETIME', [$start_time, $end_time])
            ->whereIn('TYPE', $in_type)
            ->count();

        $result['high'] = $this->where('ACCOUNT', '=', $account)
            ->where('COUNTS', '>', $max_value)
            ->whereBetween('CREATETIME', [$start_time, $end_time])
            ->whereIn('TYPE', $in_type)
            ->count();

        return $result;
    }
}
